<?php

namespace App\Admin\Forms;

use App\Models\Category;
use App\Models\Order;
use App\Models\OrderService;
use App\Models\Todo;
use App\Models\User;
use App\Support\Data;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Form as DcatForm;


class PayForms extends Form implements LazyRenderable
{
    use LazyWidget; // 使用异步加载功能

    // 处理请求
    public function handle(array $input)
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        if (!$id) {
            return $this->response()->error('参数错误');
        }

        $input['pay_status'] = 1;
        $input['order_status'] = 1;

        $result = Order::updateOrCreate(['id' => $id], $input);

        if ($result) {
            OrderService::create(['order_id' => $result->id]);
            return $this->response()->success('成功')->refresh();
        } else {
            return $this->response()->error('失败');
        }
    }

    public function form()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        $this->text('pay_amount');
        $this->image('pay_ticket')->saveFullUrl()->autoUpload();
        $this->select('pay_type')->options(Data::getPayType());
    }

    // 返回表单数据，如不需要可以删除此方法
    public function
    default()
    {
        // 获取外部传递参数
        $id = $this->payload['id'] ?? null;

        return [
            'pay_amount'         => '',
            'pay_ticket' => '',
            'pay_type' => '',
        ];
    }
}
